<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="js/jquery-1.12.4.min.js"></script>
    <script src="js/bootstrap/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody id="tbody">
                    <!-- <tr>
                        <td>未分类</td>
                        <td>uncategorized</td>
                        <td class="text-center">
                            <a href="javascript:editTr({&quot;id&quot;:&quot;1&quot;,&quot;slug&quot;:&quot;uncategorized&quot;,&quot;name&quot;:&quot;未分类&quot;});"
                                class="btn btn-info btn-xs">编辑</a>
                            <a href="javascript:deleteTr( 1 );" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>

                    <tr>
                        <td>奇趣事</td>
                        <td>funny</td>
                        <td class="text-center">
                            <a href="javascript:editTr({&quot;id&quot;:&quot;2&quot;,&quot;slug&quot;:&quot;funny&quot;,&quot;name&quot;:&quot;奇趣事&quot;});"
                                class="btn btn-info btn-xs">编辑</a>
                            <a href="javascript:deleteTr( 2 );" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>

                    <tr>
                        <td>会生活</td>
                        <td>living</td>
                        <td class="text-center">
                            <a href="javascript:editTr({&quot;id&quot;:&quot;3&quot;,&quot;slug&quot;:&quot;living&quot;,&quot;name&quot;:&quot;会生活&quot;});"
                                class="btn btn-info btn-xs">编辑</a>
                            <a href="javascript:deleteTr( 3 );" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>

                    <tr>
                        <td>爱旅行</td>
                        <td>travel</td>
                        <td class="text-center">
                            <a href="javascript:editTr({&quot;id&quot;:&quot;4&quot;,&quot;slug&quot;:&quot;travel&quot;,&quot;name&quot;:&quot;爱旅行&quot;});"
                                class="btn btn-info btn-xs">编辑</a>
                            <a href="javascript:deleteTr( 4 );" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr> -->
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <a href="#" class="btn btn-success" data-toggle="modal" data-target="#addModal">新增分类</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="modal fade" id="addModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
                    <h4 class="modal-title">新增分类</h4>
                </div>
                <div class="modal-body">
                    <form id="from">
                        <!-- //添加隐藏域 -->
                        <input type="hidden" name="id" id="hidden">
                        <div class="form-group">
                            <label for="recipient-name" class="control-label" >分类名称:</label>
                            <input type="text" class="form-control" id="catyName" name="name"> 
                        </div>
                        <div class="form-group">
                            <label for="message-text" class="control-label"  >分类别名:</label>
                            <input type="text" class="form-control" id="alisName" name='slug'>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" id="model_shutoff">关闭</button>
                    <button type="button" class="btn btn-primary" id="model_add">新增</button>
                    <button type="button" class="btn btn-primary" id="edit_add" style="display: none;">编辑</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        $('#addModal').modal({
            show: false,
            backdrop: false
        });

        $('#model_shutoff').click(function () {
            alert('shutoff');
            $('#addModal').modal('hide');
        });

        $('#model_add').click(function () {
            alert('add');
            $('#addModal').modal('hide');
        });

        // $('.category_table').delegate('a', 'click', function () {
        //     if ($(this).hasClass('btn-info')) {
        //         $('#addModal .modal-title').html('修改分类名称');
        //         $('#addModal').modal('show');
        //     }
        // })      
    </script>
    <script src="../web_front/js/js/template-web.js"></script>
    <script type="text/template" id="showTmp">
        {{each data val i}}
        <tr>
            <td>{{val.name}}</td>
            <td> {{val.slug}} </td>
            <td class="text-center">
                <a href="javascript:;" data-id='{{val.id}}' class="btn btn-info btn-xs" id="editBtn">编辑</a>
                <a href="javascript:;" class="btn btn-danger btn-xs" id="delBtn" data-id='{{val.id}}'>删除</a>
            </td>
        </tr> 
        {{/each}}
      </script>

    <script>
        $(function () {
            
              showDate();

            function showDate() {
               
                $.ajax({
                url: 'http://localhost:8000/admin/category_search',
                data: {},
                success: function (res) {
                    console.log(res);
                    //    template('id',obj)
                    var htmlStr = template('showTmp', res)
                    console.log(htmlStr);
                    
                    $('#tbody').html(htmlStr)

                }
            })
            }
            
            $('#model_add').on('click',function(){
                var catyName=$('#catyName').val().trim()
                var alisName=$('#alisName').val().trim()
                if(catyName===''||alisName===''){
                    alert('填写完整内容')
                    return;
                }
                $.ajax({
                    url:'http://localhost:8000/admin/category_add',
                    data:{
                        name:catyName ,
                        slug:alisName
                    },
                    type:'post',
                    success:function(res){
                        console.log(res);
                        if(res.code===200){
                            window.location.reload();
                        }else{
                            alert('分类名不能重复')
                        }

                        
                    }
                })

            })

            //删除功能
            $('#tbody').on('click','#delBtn',function(){
                var id=$(this).attr('data-id');
                console.log(id);
                var flag=window.confirm('确定要删除吗？')
                if(!flag){
                     return false;
                }
                $.ajax({
                    url:'http://localhost:8000/admin/category_delete',
                    type:'post',
                    data:{
                        id:id
                    },
                    success:function(res){
                        if(res.code===200){
                            alert('删除成功')
                            window.location.reload()   //重新加载当前页
                        }
                    }
                })
                
            })

            //编辑数据
            $('#tbody').on('click','#editBtn',function(){
                $('#addModal .modal-title').html('编辑数据');
                $('#addModal').modal('show');
                $('#edit_add').show();
                $('#model_add').hide();
                var id=$(this).attr('data-id');
                var thisTr=$(this).parents('tr').children();
                $('#hidden').val(id);
                $('#catyName').val($(thisTr).eq(0).text())
                $('#alisName').val($(thisTr).eq(1).text())
            })
            //提交编辑数据
            $('#edit_add').on('click',function(){
                console.log($('#from').serialize());
                $.ajax({
                    url:'http://localhost:8000/admin/category_edit',
                    data:$('#from').serialize(),
                    type:'post',
                    success:function(res){
                        console.log(res);
                        if(res.code===200){
                            window.location.reload();
                        }else{
                            alert('失败')
                        }
                        
                    }
                })
                
            })

        //     function showAdd(){
        //         $.ajax({
        //             url:'http://localhost:8000/admin/category_add',
        //             data:{
        //                 // name:
        //                 // slug:
        //             }
        //         })
        //     }
     })

    </script>

</body>

</html>